<?php
/* Marc Binns 07.07.2010
 * Collection of Battle related functions
 */
class BattleCommand{
	/**
	 * Fetch all Status_Unit rows related to the target BattleId OR "N" on failure
	 * @return json-encoded object (either "N" OR Array of Status_Unit Rows)
	 */
	public function GetBattleState_Full($battleId){
		Try{
			$params = array($battleId);
			$db = Database::getInstance();
			$return = $db->callSP(Config::sP_Battle_GetState_Full, $params);
			return $return;
			
		}catch(Exception $e){
			return "N";
		}	
	}
	/**
	 * Fetch all Status_Unit rows related to the target Player in a particular battle OR "N" on failure
	 * @return json-encoded object (either "N" OR Array of Status_Unit Rows)
	 */
	public function GetBattleState_TargetPlayer($playerId, $battleId){
		Try{
			$params = array($playerId, $battleId);
			$db = Database::getInstance();
			$return = $db->callSP(Config::sP_Battle_GetState_Player, $params);
			return $return;
			
		}catch(Exception $e){
			return "N";
		}	
	}
	/**
	 * Create a new battle instance with the specified players (Returns new battle id) 
	 *
	 * @param int $player1_Id
	 * @param int $player2_Id
	 * @return int $newBattleId
	 */
	public function CreateBattle($player1_Id, $player2_Id){
		Try{
			$newBattleId = -1;
			$params = array($player1_Id, $player2_Id);
			$db = Database::getInstance();
			$rows = $db->callSP(Config::sP_Battle_Create, $params);
			$newBattleId = intval($row["New_BattleId"]);
			 
			If ($newBattleId >= 0){
				return $newBattleId;
			}else{
				throw new Exception("Unable to create new battle!");
			}
			
		}catch(Exception $e){
			// @todo MARC: Tie BattleCommand exception Handling into common Handler
  			echo 'ERROR: ' .$e->getMessage();
		}
	}
	/**
	 * Kill a battle instance and update Player & Battle_History tables
	 *
	 * @param int $battleId
	 * @param int $playerId_Local
	 * @param int $playerId_Opponent
	 * @param char $code_Local
	 * @param char $code_Opponent
	 * @return boolean
	 */
	public function EndBattle($battleId, $playerId_Local, $playerId_Opponent, $code_Local, $code_Opponent){
		Try{
			$params = array($battleId, $playerId_Local, $playerId_Opponent, $code_Local, $code_Opponent);
			$db = Database::getInstance();
			$rows = $db->callSP(Config::sP_Battle_Kill, $params);
			$success = strval($row["Result"]);
			
			If ($success <> 'Y'){
				return false;
				//throw new Exception("Unable to End Battle!");
			}Else{
				return true;
			}
		}catch(Exception $e){
			// @todo MARC: Tie BattleCommand exception Handling into common Handler
  			echo 'ERROR: ' .$e->getMessage();
  			return false;
		}
	}
	/**
	 * Ends the turn of the specified player and resets all unit APs accordingly (Returns the New Active PlayerId)
	 *
	 * @param int $battleId
	 * @param int $playerId
	 * @return boolean
	 */
	public function EndPlayerTurn($battleId, $playerId){
		Try{
			$newActivePlayerId = -1;
			$params = array($battleId, $playerId);
			$db = Database::getInstance();
			$rows = $db->callSP(Config::sP_Battle_EndTurn, $params);
			$newActivePlayerId = intval($row["New_ActivePlayerId"]);
			
			If ($newActivePlayerId < 0){
				throw new Exception("Unable to End Turn!");
			}else{
				return $newActivePlayerId;	
			}
		}catch(Exception $e){
			// @todo MARC: Tie BattleCommand exception Handling into common Handler
  			echo 'ERROR: ' .$e->getMessage();
		}
	}

}
?>